
import os

import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import make_interp_spline
from year_data import get_every_year_avg

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

x = ['2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020']
xx = [x for x in range(0,2*len(x), 2)]
y1 = [0.12886686683259127, 0.12259952261767026, 0.1212927075142278, 0.12622560750567535, 0.1267718064921181, 0.12488795669031051, 0.13117912226135542, 0.1380507867471381, 0.13351965879869085, 0.14012666926161013, 0.13202176885000141, 0.13742586542031293, 0.1419790446722534, 0.1441797608994483, 0.14784108774455842, 0.15134892207298456, 0.15299477264278058, 0.15692624141547487, 0.14453222526861362, 0.1430807860207258, 0.15652755895222933]
y2 = [0.152662805744296, 0.1564517304173288, 0.14729035340732458, 0.1610168415035425, 0.15574523403806445, 0.1623285998775294, 0.16214285585799318, 0.16671402999178264, 0.16667904309438208, 0.16931895539927308, 0.1656364372746521, 0.17359957170250617, 0.1718110318558264, 0.17401952051952094, 0.172492366891997, 0.17986795026694932, 0.17706883732246406, 0.18463072152257246, 0.17006728969217835, 0.1761421870935404, 0.17767137193216245]



def draw(data_dir, fig, params):
    x = ['2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013',
         '2014', '2015', '2016', '2017', '2018', '2019', '2020']
    xx = [x for x in range(0, 2 * len(x), 2)]
    xn = np.array(xx)
    x_smooth = np.linspace(xn.min(), xn.max(), 300)
    y = get_every_year_avg(data_dir)
    print(y)
    y_year_avg = y['year_avg']
    y_growth_avg = y['growth_avg']
    y_year_avg_smooth = make_interp_spline(xn, y_year_avg )(x_smooth)
    y_growth_avg_smooth = make_interp_spline(xn, y_growth_avg)(x_smooth)
    plt__ = fig.add_subplot(params)
    plt__.plot(x_smooth, y_year_avg_smooth, label="%sNDVI年度平均变化趋势图" % os.path.basename(data_dir))
    plt__.plot(x_smooth, y_growth_avg_smooth, label="%sNDVI生长期（4-10月）平均变化趋势图" % os.path.basename(data_dir))
    # plt__.xticks(xx, labels=x)
    # plt__.xlabel("years")
    # plt__.ylabel("NDVI")
    # plt__.grid(alpha=0.1, color='g', which="major")
    # plt__.legend()
    # plt.show()


def run():
    root_dir = r"E:\毕设数据\UsingTypeNDVI_Excel"
    dirs = os.listdir(root_dir)
    c = 321
    fig = plt.figure(figsize=(15,12))
    for i in dirs:
        d = os.path.join(root_dir, i)
        draw(d, fig, c)
        c +=1

    plt.show()



if __name__ == '__main__':
    run()